//
//  CJExten+Frame.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-04.
//

import UIKit
import Foundation

public extension UIView {
    
    /// x
    @objc var cj_x: CGFloat {
        get {
            return self.frame.origin.x
        }
        set {
            var rect = self.frame
            rect.origin.x = newValue
            self.frame = rect
        }
    }
    
    /// y
    @objc var cj_y: CGFloat {
        get {
            return self.frame.origin.y
        }
        set {
            var rect = self.frame
            rect.origin.y = newValue
            self.frame = rect
        }
    }
    
    /// 定x的right_x(固定控件x值 取赋右边值 改变宽度)
    @objc var cj_right: CGFloat {
        get {
            return self.frame.origin.x + self.frame.size.width
        }
        set {
            var rect = self.frame
            rect.size.width = newValue - rect.origin.x
            self.frame = rect
        }
    }
    
    /// 定width的right_x(固定控件宽度 取赋右边值 改变x值)
    @objc var cj_right_x: CGFloat {
        get {
            return self.frame.origin.x + self.frame.size.width
        }
        set {
            var rect = self.frame
            rect.origin.x = newValue - rect.size.width
            self.frame = rect
        }
    }
    
    /// 定y的bottom_y(固定控件y值 取赋下边值 改变高度)
    @objc var cj_bottom: CGFloat {
        get {
            return self.frame.origin.y + self.frame.size.height
        }
        set {
            var rect = self.frame
            rect.size.height = newValue - rect.origin.y
            self.frame = rect
        }
    }
    
    /// 定height的bottom_y(固定控件高度 取赋下边值 改变y值)
    @objc var cj_bottom_y: CGFloat {
        get {
            return self.frame.origin.y + self.frame.size.height
        }
        set {
            var rect = self.frame
            rect.origin.y = newValue - rect.size.height
            self.frame = rect
        }
    }
    
    /// width
    @objc var cj_width: CGFloat {
        get {
            return self.frame.size.width
        }
        set {
            var rect = self.frame
            rect.size.width = newValue
            self.frame = rect
        }
    }
    
    /// 定right_x的width(固定控件右边值 取赋宽度 改变x值)
    @objc var cj_width_r: CGFloat {
        get {
            return self.frame.size.width
        }
        set {
            var rect = self.frame
            rect.origin.x = rect.origin.x - (newValue - rect.size.width)
            self.frame = rect
        }
    }
    
    /// height
    @objc var cj_height: CGFloat {
        get {
            return self.frame.size.height
        }
        set {
            var rect = self.frame
            rect.size.height = newValue
            self.frame = rect
        }
    }
    
    /// 定bottom_y的height(固定控件下边值 取赋高度 改变y值)
    @objc var cj_height_b: CGFloat {
        get {
            return self.frame.size.height
        }
        set {
            var rect = self.frame
            rect.origin.y = rect.origin.y - (newValue - rect.size.height)
            self.frame = rect
        }
    }
    
    /// centerX
    @objc var cj_centerX: CGFloat {
        get {
            return self.center.x
        }
        set {
            self.center = CGPoint(x: newValue, y: self.center.y)
        }
    }
    
    /// centerY
    @objc var cj_centerY: CGFloat {
        get {
            return self.center.y
        }
        set {
            self.center = CGPoint(x: self.center.x, y: newValue)
        }
    }
    
    /// origin
    @objc var cj_origin: CGPoint {
        get {
            return self.frame.origin
        }
        set {
            var rect = self.frame
            rect.origin = newValue
            self.frame = rect
        }
    }
    
    /// size
    @objc var cj_size: CGSize {
        get {
            return self.frame.size
        }
        set {
            var rect = self.frame
            rect.size = newValue
            self.frame = rect
        }
    }
}
